package com.bit.catmoney;

import java.util.Random;

import com.bit.catmoney.R;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;

public class OptActivity extends Activity implements OnCheckedChangeListener {
	public ToggleButton toggle1;
	public ToggleButton toggle2;
	Random random = new Random();
	int [] arrbgm = {R.raw.snowman_bgm,R.raw.fun_bgm,};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_opt);
//		MediaPlayer  mPlayer = MediaPlayer.create(this, R.raw.sb_bgm);   // ���� �б� rondo�� ���ϸ�
//	      mPlayer.setVolume(0.8f, 0.8f);        // ���� ����
//	      mPlayer.setLooping(true);               // �ݺ� ����
//	      mPlayer.start();                            // (������ ��ġ���� ����) ��� ����
//
//	      mPlayer.stop();                                  // ��� ����
//
//	      mPlayer.pause();

		toggle1 = (ToggleButton) findViewById(R.id.tggbtn1);
		toggle1.setOnCheckedChangeListener(this);
		toggle1.setChecked(getDefaults("etatToggle1", this));
		setDefaults("etatToggle1", toggle1.isChecked(), this);
		
		toggle2 = (ToggleButton) findViewById(R.id.tggbtn2);
		toggle2.setOnCheckedChangeListener(this);
		toggle2.setChecked(getDefaults("etatToggle2", this));
		setDefaults("etatToggle2", toggle2.isChecked(), this);

	}

	public static void setDefaults(String key, Boolean value, Context context) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	public static Boolean getDefaults(String key, Context context) {
		SharedPreferences preferences = PreferenceManager
				.getDefaultSharedPreferences(context);
		return preferences.getBoolean(key, true);
	}

	@Override
	protected void onStart() {
		super.onStart();
		initCheckButton();
	}

	@Override
	protected void onPause() {
		super.onPause();
		initCheckButton();	

	}

	@Override
	protected void onStop() {
		super.onStop();
		initCheckButton();	
	}

	private void initCheckButton() {
		if(toggle1.getId() == R.id.tggbtn1){
			setDefaults("etatToggle1", toggle1.isChecked(), this);
		}else if(toggle2.getId() == R.id.tggbtn2){
			setDefaults("etatToggle2", toggle2.isChecked(), this);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.opt, menu);
		return true;
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		switch (buttonView.getId()) {
		case R.id.tggbtn1:
			Log.d("testaaa", isChecked + "");
			if (toggle1.isChecked()) {
				setDefaults("etatToggle1", toggle1.isChecked(), this);
				CMusic.getInstance().loopStart(OptActivity.this, arrbgm[random.nextInt(2)]);
			} else {
				setDefaults("etatToggle1", toggle1.isChecked(), this);
				CMusic.getInstance().stop();
			}
			break;
		case R.id.tggbtn2:
			Log.d("testbbb", isChecked + "");
			if (toggle2.isChecked()) {

				setDefaults("etatToggle2", toggle2.isChecked(), this);
				CFxSound.setIsPlay(true);
			} else {

				setDefaults("etatToggle2", toggle2.isChecked(), this);
				CFxSound.setIsPlay(false);
			}
			break;

		}

	}

	
}
